/**
 * 
 */
package br.srv.full.virtual.faces.manager;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.event.PhaseEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.srv.full.service.ServiceLocator;
import br.srv.full.virtual.engine.users.PersonsManager;
import br.srv.full.virtual.entities.auth.User;

/**
 * @author Delfino
 * 
 */
public class ApplicationBackBean {

	private final Log log = LogFactory.getLog(getClass());

	// @EJB(mappedName="PersonsManager/local")
	private PersonsManager usersManager;

	{
		usersManager = ServiceLocator.getService(PersonsManager.class);
		log.info(usersManager);
	}
	/**
	 * 
	 */
	private String posTitle;

	/**
	 * 
	 */
	private String preTitle;

	/**
	 * 
	 */
	private String title;

	{
		log.info("Inicializador Default!");
	}

	/**
	 * 
	 */
	public ApplicationBackBean() {
		log.info("Criado application");
	}

	/**
	 * 
	 */
	@PostConstruct
	public void initializer() {
		log.info("pos constructor");
		if (!existBaseUser()) {
			createBaseUser();
		}
	}

	private boolean existBaseUser() {
		try {
			usersManager.findUserLogin(User.FindByEMail, "admin@full.srv.br");
			// } catch (NoResultException e) { // não usar objetos da
			// persistencia neste nivel.
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	private void createBaseUser() {
		// TODO Auto-generated method stub
		log.info("Aqui será criado os beans padrões.");
	}

	/**
	 * 
	 */
	@PreDestroy
	public void destroier() {
		log.info("Destroier");
	}

	/**
	 * @return
	 */
	public String getFullTitle() {
		return getPreTitle() + getTitle() + getPosTitle();
	}

	/**
	 * @return the posTitle
	 */
	public String getPosTitle() {
		return posTitle;
	}

	/**
	 * @return the preTitle
	 */
	public String getPreTitle() {
		return preTitle;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param posTitle
	 *            the posTitle to set
	 */
	public void setPosTitle(String posTitle) {
		this.posTitle = posTitle;
	}

	/**
	 * @param preTitle
	 *            the preTitle to set
	 */
	public void setPreTitle(String preTitle) {
		this.preTitle = preTitle;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @param _event
	 */
	public void beforePhase(PhaseEvent _event) {
		log.info(_event);
	}

	/**
	 * @param _event
	 */
	public void afterPhase(PhaseEvent _event) {
		log.info(_event);
	}
}
